﻿namespace System.ServiceModel.DomainServices.Client
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Runtime.Serialization;

    [DebuggerDisplay("Operation = {Operation}, Type = {Entity.GetType().Name}"), DataContract(Namespace="DomainServices")]
    public sealed class ChangeSetEntry
    {
        private System.ServiceModel.DomainServices.Client.Entity _clientEntity;
        private System.ServiceModel.DomainServices.Client.Entity _entity;
        private bool _hasMemberChanges;
        private int _id;
        private EntityOperationType _operationType;
        private System.ServiceModel.DomainServices.Client.Entity _originalEntity;
        private System.ServiceModel.DomainServices.Client.Entity _storeEntity;

        public ChangeSetEntry(System.ServiceModel.DomainServices.Client.Entity entity, int id, EntityOperationType operationType)
        {
            this._entity = entity;
            this._id = id;
            this._operationType = operationType;
        }

        [DataMember(EmitDefaultValue=false)]
        public IDictionary<string, int[]> Associations { get; set; }

        public System.ServiceModel.DomainServices.Client.Entity ClientEntity
        {
            get
            {
                return (this._clientEntity ?? this._entity);
            }
            set
            {
                this._clientEntity = value;
            }
        }

        [DataMember(EmitDefaultValue=false)]
        public IEnumerable<string> ConflictMembers { get; set; }

        [DataMember]
        public System.ServiceModel.DomainServices.Client.Entity Entity
        {
            get
            {
                return this._entity;
            }
            set
            {
                this._entity = value;
            }
        }

        [DataMember]
        public IDictionary<string, object[]> EntityActions { get; set; }

        public bool HasConflict
        {
            get
            {
                return (this.IsDeleteConflict || ((this.ConflictMembers != null) && this.ConflictMembers.Any<string>()));
            }
        }

        internal bool HasError
        {
            get
            {
                return (this.HasConflict || ((this.ValidationErrors != null) && this.ValidationErrors.Any<ValidationResultInfo>()));
            }
        }

        [DataMember]
        public bool HasMemberChanges
        {
            get
            {
                return this._hasMemberChanges;
            }
            set
            {
                this._hasMemberChanges = value;
            }
        }

        [DataMember]
        public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        [DataMember(EmitDefaultValue=false)]
        public bool IsDeleteConflict { get; set; }

        [DataMember]
        public EntityOperationType Operation
        {
            get
            {
                return this._operationType;
            }
            set
            {
                this._operationType = value;
            }
        }

        [DataMember(EmitDefaultValue=false)]
        public IDictionary<string, int[]> OriginalAssociations { get; set; }

        [DataMember(EmitDefaultValue=false)]
        public System.ServiceModel.DomainServices.Client.Entity OriginalEntity
        {
            get
            {
                return this._originalEntity;
            }
            set
            {
                this._originalEntity = value;
                if (value != null)
                {
                    this._hasMemberChanges = true;
                }
            }
        }

        [DataMember(EmitDefaultValue=false)]
        public System.ServiceModel.DomainServices.Client.Entity StoreEntity
        {
            get
            {
                return this._storeEntity;
            }
            set
            {
                this._storeEntity = value;
            }
        }

        [DataMember(EmitDefaultValue=false)]
        public IEnumerable<ValidationResultInfo> ValidationErrors { get; set; }
    }
}

